<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserLevel;
use app\services\Chain;

class Up
{
    protected $order;
    // protected $set;

    public function __construct(Order $order) {
        $this->order = $order;
        $this->set = SetUserUp::getLastOne();

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 只有报单区才可以升级
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])
        || ProductAreaList::checkInArea($product_id, Product::$areaMap['R'])) {
            // 后台设置的参数
            // $this->set = SetUserUp::getLastOne();


            // 升级
            $this->up();

            // 数量升级推荐人
            $this->upCount();

            // $this->upChain();
        }
    }

    protected function up()
    {
        $total_price = $this->order->total_pay - $this->order->total_freight;

        $levels = UserLevel::find()
            ->where(['>', 'price', 0])
            ->andWhere(['>', 'value', 0])
            ->orderBy(['value' => SORT_DESC])
            ->asArray()
            ->all();

        // 根据价格升级相应等级 [自己升级]
        foreach ($levels as $level) {
            if ($total_price - $level['price'] >= 0) {

                $user = User::findOne(['id' => $this->order->user_id]);
                if ($user && $user->level < $level['value']) {
                    $user->level = $level['value'];
                    $user->save();
                    break;
                }
            }
        }


    }

    // 根据关系链升级 , 就是推荐 6 个SVIP升级到合伙人
    private function upChain()
    {
        $chain = Chain::getReferrerChain($this->order->user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
        ], true);

        foreach ($chain as $user) {

        }
    }

    // 根据推荐数量, 升级推荐人
    private function upCount()
    {
        $r = Chain::getReferrer($this->order->user_id, true);

        if ($r) {

            $set = SetUserUp::getLastOne();

            // SVIP
            if ($r->level == UserLevel::$keys['vip2']) {

                // 升分公司
                $vip2 = User::find()
                    ->where(['referrer_id' => $r->id])
                    ->andWhere(['level' => [UserLevel::$keys['vip2'], UserLevel::$keys['vip3']]])
                    ->count();
                if ($vip2 >= $set->vip3_2) {
                    $r->level = UserLevel::$keys['vip3'];
                    $r->save();
                    return;
                }
            }

            // VIP
            if ($r->level == UserLevel::$keys['vip1']) {

                // 升分公司
                $vip2 = User::find()
                    ->where(['referrer_id' => $r->id])
                    ->andWhere(['level' => [UserLevel::$keys['vip2'], UserLevel::$keys['vip3']]])
                    ->count();
                if ($vip2 >= $set->vip3_2) {
                    $r->level = UserLevel::$keys['vip3'];
                    $r->save();
                    return;
                }

                // 升SVIP
                $vip1 = User::find()
                    ->where(['referrer_id' => $r->id])
                    ->andWhere(['level' => [UserLevel::$keys['vip1'], UserLevel::$keys['vip2'], UserLevel::$keys['vip3']]])
                    ->count();
                if ($vip1 >= 2) {
                    $r->level = UserLevel::$keys['vip2'];
                    $r->save();
                    return;
                }
            }

        }
    }

}